//
//  DynamicTableViewCell.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/2.
//

#import "DynamicTableViewCell.h"

@interface DynamicTableViewCell ()<PhotosViewDelegate>

@property (nonatomic, strong) UIImageView *avatarImageV;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *editBtn;
@property (nonatomic, strong) UIButton *forwardBtn;
@property (nonatomic, strong) UIButton *updateBtn;

@end

@implementation DynamicTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    [self.contentView addSubview:self.avatarImageV];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.photosView];
    [self.contentView addSubview:self.priceLabel];
    [self.contentView addSubview:self.lineView];
    [self.contentView addSubview:self.editBtn];
    [self.contentView addSubview:self.forwardBtn];
    [self.contentView addSubview:self.updateBtn];
}

- (UIImageView *)avatarImageV {
    if (!_avatarImageV) {
        _avatarImageV = [[UIImageView alloc] init];
        _avatarImageV.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImageV.layer.cornerRadius = 20;
        _avatarImageV.layer.masksToBounds = YES;
        _avatarImageV.userInteractionEnabled = YES;
        [_avatarImageV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goshopAction:)]];
    }
    return _avatarImageV;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = kRGB(0x333333);
        _nameLabel.font = kFontMediium(14);
        [_nameLabel sizeToFit];
    }
    return _nameLabel;
}
- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.textColor = kRGB(0x999999);
        _timeLabel.font = kFontRegula(10);
        [_timeLabel sizeToFit];
    }
    return _timeLabel;
}
- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.textColor = kRGB(0x666666);
        _contentLabel.font = kFontRegula(13);
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}
- (PhotosView *)photosView {
    if (!_photosView) {
        CGFloat photoW = self.bounds.size.width - 64 - 16;
        _photosView = [PhotosView photosViewWithWidth:photoW margin:10];
        _photosView.delegate = self;
    }
    return _photosView;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.textColor = kRGB(0xF15757);
        _priceLabel.font = kFontMediium(16);
        [_priceLabel sizeToFit];
    }
    return _priceLabel;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kRGB(0xF3F3F3);
    }
    return _lineView;
}

- (UIButton *)forwardBtn {
    if (!_forwardBtn) {
        _forwardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_forwardBtn setTitle:@"一键转图" forState:UIControlStateNormal];
        [_forwardBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _forwardBtn.titleLabel.font = kFontMediium(12);
        _forwardBtn.backgroundColor = kBgColor;
        _forwardBtn.layer.cornerRadius = 12;
        _forwardBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_forwardBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.transBlock) {
                self.transBlock(self.frameModel.model);
            }
        }];
    }
    return _forwardBtn;
}

- (UIButton *)editBtn {
    if (!_editBtn) {
        _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_editBtn setTitle:@"编辑转图" forState:UIControlStateNormal];
        [_editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _editBtn.titleLabel.font = kFontMediium(12);
        _editBtn.backgroundColor = kRGB(0xFF6081);
        _editBtn.layer.cornerRadius = 12;
        _editBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_editBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.editTransBlock) {
                self.editTransBlock(self.frameModel.model);
            }
        }];
    }
    return _editBtn;
}

- (UIButton *)updateBtn {
    if (!_updateBtn) {
        _updateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _updateBtn.backgroundColor = kRGB(0xFFD937);
        [_updateBtn setTitle:@"同步更新" forState:UIControlStateNormal];
        [_updateBtn setTitleColor:kRGB(0x333333) forState:UIControlStateNormal];
        _updateBtn.titleLabel.font = kFontMediium(12);
        _updateBtn.layer.cornerRadius = 12;
        _updateBtn.layer.masksToBounds = YES;
        @weakify(self)
        [[_updateBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self)
            if (self.updateGoodsBlock) {
                self.updateGoodsBlock(self.frameModel.model);
            }
        }];
    }
    return _updateBtn;
}

- (void)setFrameModel:(ShopModelFrame *)frameModel {
    _frameModel = frameModel;
    [self.avatarImageV sd_setImageWithURL:[NSURL URLWithString:frameModel.model.avatar] placeholderImage:[UIImage imageNamed:@"avatar"]];
    self.nameLabel.text = frameModel.model.nickName;
    self.timeLabel.text = [NSDate formateData:frameModel.model.createdAt];
    self.contentLabel.text = frameModel.model.goodsIntroduction;
    self.priceLabel.text = [NSString stringWithFormat:@"¥ %@",frameModel.model.sellingPrice];
    self.editBtn.hidden = self.forwardBtn.hidden = frameModel.model.trans;
    self.updateBtn.hidden = !(frameModel.model.trans && frameModel.model.hasUpdate);
    [self updateFrame];
}

- (void)updateFrame {
    self.avatarImageV.frame = self.frameModel.iconF;
    self.nameLabel.frame = self.frameModel.nameF;
    self.timeLabel.frame = self.frameModel.timeF;
    self.contentLabel.frame = self.frameModel.contentF;
    [self.photosView updateWidth:(self.frameModel.width - 64 - 16)];
    self.photosView.frame = self.frameModel.photosF;
    self.photosView.images = self.frameModel.model.imageModels;
    self.priceLabel.frame = self.frameModel.priceF;
    self.lineView.frame = self.frameModel.lineF;
    
    [self.updateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-16);
        make.centerY.equalTo(self.priceLabel);
        make.size.mas_equalTo(CGSizeMake(64, 24));
    }];

    [self.forwardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-16);
        make.centerY.equalTo(self.priceLabel);
        make.size.mas_equalTo(CGSizeMake(64, 24));
    }];
    
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.forwardBtn.mas_leading).offset(-10);
        make.centerY.equalTo(self.priceLabel);
        make.size.mas_equalTo(CGSizeMake(64, 24));
    }];
}

- (void)photosTaped:(UIImageView *)imgV {
    if (self.photosViewClick) {
        self.photosViewClick(self, imgV.tag);
    }
}

- (void)goshopAction:(UITapGestureRecognizer *)sender {
    if (self.goShopBlock) {
        self.goShopBlock(self.frameModel.model);
    }
}

@end
